<template>
  <div class="container">
    <el-card shadow="never">
      <div class="search_content">
        <el-form label-width="100px">
          <el-form-item label="素材类型：" class="form-item">
            <el-radio-group
              v-model="formData.type"
              class="radio-group"
              @change="search"
            >
              <el-radio
                v-for="(label, value) in typeEnums"
                :key="value"
                :label="+value"
              >
                {{ label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="状态：" class="form-item">
            <el-radio-group
              v-model="formData.status"
              class="radio-group"
              @change="search"
            >
              <el-radio
                v-for="(label, value) in timeEnums"
                :key="value"
                :label="+value"
              >
                {{ label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>

          <!-- <el-form-item label="素材名称：" class="form-item">
              <el-input
                v-model="formData.material_name"
                placeholder="请输入素材名称"
                style="width: 400px"
              />
            </el-form-item> -->

          <!-- <el-form-item class="form-item">
              <el-button type="primary" @click="search">搜索</el-button>
              <el-button @click="reset">重置</el-button>
            </el-form-item> -->
        </el-form>
        <el-button
          type="primary"
          style="margin-bottom: 15px"
          @click="handelAddServe"
        >
          添加
        </el-button>
      </div>

      <div v-loading="loading">
        <el-row :gutter="20">
          <el-col :span="6" v-for="item in dataSource" :key="item.detail_id">
            <div class="card-box">
              <BaseImg :src="item.material_cover" class="upload-img" />
              <!-- <el-image
                :preview-src-list="[
                  $store.getters.imgURL + item.material_cover
                ]"
                :src="$store.getters.imgURL + item.material_cover"
              /> -->
              <p>素材名称：{{ item.material_name }}</p>
              <div class="tools">
                <i class="el-icon-edit" @click="handelAddServe(item)" />
                <i class="el-icon-delete" @click="goDelete(item)" />
              </div>
            </div>
          </el-col>
        </el-row>

        <el-pagination
          :current-page="page.currentPage"
          :page-sizes="page.pageSizes"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          :hide-on-single-page="page.total < page.size ? true : false"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import copyrightApi from '@/api/copyright'
import { typeEnums, timeEnums } from '@/enums/copyright'
import BaseImg from '@/components/baseImg'
export default {
  components: { BaseImg },
  data() {
    return {
      loading: false,
      checkList: [],
      typeEnums,
      timeEnums,
      dataSource: [],
      addShow: false,
      val: {},
      formData: {
        type: 0,
        status: 0,
        material_name: ''
      },
      page: {
        total: 0,
        currentPage: 1,
        size: 12,
        pageSizes: [12, 24, 48, 60]
      } // 分页器
    }
  },
  mounted() {
    this.getListData()
  },
  methods: {
    reset() {
      this.formData = {
        type: 0,
        status: 0,
        material_name: ''
      }
      this.getListData()
    },
    search() {
      this.val.page = 1
      this.val.limit = 12
      this.getListData()
    },
    handelAddServe(row = {}) {
      this.$router.push({
        name: 'addCopyright',
        query: {
          id: row.copyright_id
        }
      })
    },
    async getListData() {
      this.loading = true
      this.val = this.formData
      this.val.page = this.page.currentPage
      this.val.limit = this.page.size

      const res = await copyrightApi.copyrightList(this.val)
      if (res.code == 0) {
        this.dataSource = res.data
        this.page.total = res.count
      }
      this.loading = false
    },
    async goDelete(row) {
      const result = await this.$confirm(
        '此操作将永久删除该数据，是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(() => {})
      if (result == 'confirm') {
        const res = await copyrightApi.deleteCopyright({
          detail_id: [row.detail_id]
        })
        if (res.code == 0) {
          this.getListData()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }
    },
    // 分页规格改变
    handleSizeChange(val) {
      this.page.size = val
      this.page.currentPage = 1
      this.getListData()
    },
    // 分页点击
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getListData()
    }
  }
}
</script>

<style lang="scss">
.radio-group {
  .el-radio__input {
    display: none !important;
  }
}
</style>
<style lang="scss" scoped>
.el-col {
  margin-bottom: 20px;
}
.form-item {
  margin-bottom: 5px;
}
.el-image {
  height: 200px;
  width: 100%;
}
.card-box {
  border: 1px solid #eee;
  padding: 20px;
  position: relative;
  p {
    font-size: 14px;
    padding-top: 10px;
  }
  .tools {
    position: absolute;
    right: 20px;
    bottom: 20px;
    i {
      margin-left: 20px;
      cursor: pointer;
    }
  }
}
.search_content {
  margin-bottom: 30px;
}
.search_content {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.margin-t {
  margin-top: 30px;
}

.el-radio {
  margin-right: 20px;
}
</style>
